﻿
package test {
	import extremefx.DateTimeKind;	
	import extremefx.TimeZone;	
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import extremefx.DateTime;
	import extremefx.unitTest.Assert;	

	/**
	 * @author Marcelo Volmaro
	 */
	 
	[Frame(factoryClass="extremefx.Preloader")];
	public final class TimeZoneTest extends Sprite {

		private var _tf:TextField;
		
		public function main():void{
			var tf:TextField = _tf = new TextField();
			var tfmt:TextFormat = new TextFormat("arial unicode ms", 14, 0);
			stage.addEventListener(Event.RESIZE, resizeStage);
			addChild(tf);
			tf.x = 0;
			tf.y = 0;
			tf.width = stage.stageWidth;
			tf.height = stage.stageHeight;
			tf.defaultTextFormat = tfmt;
			Assert.setOutput(tf);
						
			var tz:TimeZone = new TimeZone(25);//Argentina, -3gmt, no DST
			var gmtDate:DateTime = new DateTime(1972, 5, 28, 10, 0, 0, 0, DateTimeKind.UTC);
			var localDate:DateTime = new DateTime(1972, 5, 28, 7, 0, 0, 0, DateTimeKind.LOCAL);
			//this is supposed to be at GMT time. 62211492000000 ticks
			var convertedLocal:DateTime = tz.toLocalTime(gmtDate);

			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(gmtDate).ticks);//no conversion
			Assert.areEquals(localDate.ticks, convertedLocal.ticks);//to local => - 3 * 60 * 60 * 1000
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(localDate).ticks);//from local => + 3 * 60 * 60 * 1000
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(convertedLocal).ticks);//conversion from <-> to

			tz = new TimeZone(3);//Alaska, -9gmt, 2 programatic DST years, 2006 & 2007 
			//starts daylight 2006 first sunday of april at 2:00:00 ST(2006/10/29)
			//ends daylight 2006 last sunday of october at 2:00:00 DT(2006/4/2)
			
			//starts daylight 2007 second sunday of march at 2:00:00 (2007/11/4)
			//ends daylight 2007 first sunday of november at 2:00:00 (2007/3/4)
			
			gmtDate = new DateTime(2006, 10, 29, 9, 59, 59, 0, DateTimeKind.UTC);//inside DLS.
			localDate = new DateTime(2006, 10, 29, 1, 59, 59, 0, DateTimeKind.LOCAL);//inside DLS.
			//8 hours difference...
			convertedLocal = tz.toLocalTime(gmtDate);
			
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(gmtDate).ticks);//No conversion
			Assert.areEquals(localDate.ticks, convertedLocal.ticks);
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(localDate).ticks);
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(convertedLocal).ticks);//conversion from <-> to
			
			Assert.isTrue(tz.isDaylightSavingTime(gmtDate));
			Assert.isTrue(tz.isDaylightSavingTime(localDate));
			
			gmtDate = new DateTime(2006, 4, 2, 10, 59, 59, 0, DateTimeKind.UTC);//outside DLS.
			localDate = new DateTime(2006, 4, 2, 1, 59, 59, 0, DateTimeKind.LOCAL);//outside DLS.
			//9 hours difference...
			convertedLocal = tz.toLocalTime(gmtDate);
			
			Assert.isFalse(tz.isDaylightSavingTime(gmtDate));
			Assert.isFalse(tz.isDaylightSavingTime(localDate));
			
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(gmtDate).ticks);//No conversion
			Assert.areEquals(localDate.ticks, convertedLocal.ticks);
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(localDate).ticks);
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(convertedLocal).ticks);//conversion from <-> to
			
			gmtDate = new DateTime(2006, 4, 2, 11, 0, 0, 0, DateTimeKind.UTC);//inside DLS.
			localDate = new DateTime(2006, 4, 2, 3, 0, 0, 0, DateTimeKind.LOCAL);//inside DLS.
			//8 hours difference...
			convertedLocal = tz.toLocalTime(gmtDate);
			
			Assert.isTrue(tz.isDaylightSavingTime(gmtDate));
			Assert.isTrue(tz.isDaylightSavingTime(localDate));
			
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(gmtDate).ticks);//No conversion
			Assert.areEquals(localDate.ticks, convertedLocal.ticks);
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(localDate).ticks);
			Assert.areEquals(gmtDate.ticks, tz.toUniversalTime(convertedLocal).ticks);//conversion from <-> to
		}
		
		private function resizeStage(event:Event):void {
			_tf.width = stage.stageWidth;
			_tf.height = stage.stageHeight;
		}
	}
}
